import Vue from 'vue'
import VueRouter from 'vue-router'
import {menuRouter} from "@/router/modules/system";
import store from '@/store/index'
import {checkToken} from "@/api/system";
// import {setJwtToken} from "@/lib/utils/TokenUtil";

Vue.use(VueRouter)
const Home = () => import('@/views/Home.vue')
const Login = () => import('@/views/Login.vue')

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name:'Home',
    component: Home,
    children:[
        ...menuRouter
    ]
  }
]

const router = new VueRouter({
  routes
})

// 全局路由后置守卫 当路由跳转后 做对应的添加tab操作
router.afterEach((to) => {
  if (to.path!=='/'&&to.path!==''&&to.path!=='/login'){
    store.commit('addTab',to.path)
  }
})

router.beforeEach((to,from,next)=>{
  if (to.name === 'Login'){
    next()
  }else{
    checkToken().then(()=>{
      // console.log(res)
      console.log("全局前置路由守卫:放行")
      next()
    }).catch(err=>{
      console.log('全局前置路由守卫:errMsg:'+err);
    })
  }
})
/*router.beforeEach((to,from,next)=>{
  if (to.name === 'Login'){
    next()
  }else{
    refreshToken().then(res=>{
      //如果token刷新成功
        // 将新获得的token保存起来
        setJwtToken(res.data)
        next()
    }).catch(err=>{
      console.log(err.message)
      setJwtToken('')
      next('/login')
    })
  }
})*/
export default router
